﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Reserve
{
    public class AutoClickManager
    {
        private static MainWindow mainWindow;

        public static ManualResetEventSlim InitialDoor = new ManualResetEventSlim(false);

        private static Dictionary<AutoClick, Thread> autoClickWorkers = new Dictionary<AutoClick, Thread>();
        private static List<Thread> checkers = new List<Thread>();
        private static Thread datePicker;

        public static void StartAutoClick(MainWindow window, ConfirmMode confirmMode)
        {
            mainWindow = window;

            for (int i = 0; i < ConfigHelper.Concurrent; i++)
            {
                AutoClick autoClick = new AutoClick(confirmMode);
                Thread worker = new Thread(autoClick.DoWork);
                worker.Start();
                autoClickWorkers.Add(autoClick, worker);
            }

            if (confirmMode == ConfirmMode.SignalMode)
            {
                for (int i = 0; i < ConfigHelper.Checker; i++)
                {
                    Thread checker = new Thread(AutoClickChecker.DoCheck);
                    checker.Start();
                    checkers.Add(checker);
                    AutoClickChecker.StatusDic.Add(checker, false);
                }

                datePicker = new Thread(AutoClickDatePicker.DoPickDate);
                datePicker.Start();
            }

            InitialDoor.Set();
        }


        private static object syncObj = new object();
        private static bool gotIt = false;

        public static void GotIt()
        {
            lock (syncObj)
            {
                if (gotIt == false)
                {
                    gotIt = true;
                    mainWindow.Dispatcher.BeginInvoke(new Action(mainWindow.GotIt));
                }
            }
        }

        public static void AbortAllThread()
        {
            foreach (var item in checkers)
            {
                item.Abort();
            }
            if (datePicker != null)
            {
                datePicker.Abort();
            }
            foreach (var item in autoClickWorkers.Values)
            {
                item.Abort();
            }
        }

        public static CheckContext GetOneCheckContext()
        {
            Random rand = new Random();
            List<CheckContext> contexts = new List<CheckContext>();
            foreach (var item in autoClickWorkers.Keys)
            {
                CheckContext context = item.ContextForCheck;
                if (context != null)
                {
                    contexts.Add(context);
                }
            }

            int count = contexts.Count;
            if (count == 0)
            {
                return null;
            }
            else
            {
                return contexts[rand.Next(count)];
            }
        }

        public static string GetOneLoginedCookie()
        {
            if (AutoClickChecker.CheckDoor.IsSet == true && AutoClickDatePicker.PickDateDoor.IsSet == true)
            {
                return string.Empty;
            }

            AutoClick autoClick = null;
            string cookie = string.Empty;

            foreach (var item in autoClickWorkers.Keys)
            {
                cookie = item.Cookie;
                if (cookie.Contains("ds01") == true)
                {
                    autoClick = item;
                    break;
                }
            }

            if (autoClick != null)
            {
                autoClickWorkers[autoClick].Abort();
                Thread worker = new Thread(autoClick.DoWork);
                autoClickWorkers[autoClick] = worker;
                worker.Start();

                return cookie;
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
